package com.bkhc.product.api;

import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.annotation.ApiPackage;
import com.bkhc.common.annotation.AuditLog;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.ExcelUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.product.entity.*;
import com.bkhc.product.service.BrandService;
import com.bkhc.product.service.PriceService;
import com.bkhc.product.service.ProductService;
import com.bkhc.product.vo.BrandGoodsVO;
import com.bkhc.product.vo.ProductPriceVO;
import com.bkhc.product.vo.SaveBrandGoodsVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
@Api("品牌管理服务")
@ApiPackage(code="brand-api",name="品牌管理服务",description = "后台品牌管理维护操作接口")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private PriceService priceService;

    @Autowired
    private ProductService productService;

    @ApiOperation(code="findBrand",name="查询品牌",description = "查询品牌",type = "common")
    @AuditLog(object="Brand",operation = "查询品牌",type = OperationEnum.query)
    @RequestMapping(path = "/list/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public ResultJson<?> findBrand(@RequestBody(required=false) Brand brand, @PathVariable(required=false,name="pageNum") int pageIndex, @PathVariable(required=false,name="pageSize") int pageSize) {
        ResultJson<?> res = brandService.findBrand(brand, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="updateBrand",name="修改品牌",description = "修改品牌")
    @AuditLog(object="Brand",operation = "修改品牌",type = OperationEnum.update)
    @RequestMapping(path = "/Brand/update",method = RequestMethod.POST)
    public ResultJson<?> updateBrand(@RequestBody(required=false) Brand brand) {
        ResultJson<?> res = brandService.updateBrand(brand);
        return res;
    }

    @ApiOperation(code="deleteBrand",name="删除品牌",description = "删除品牌")
    @AuditLog(object="Brand",operation = "删除品牌",type = OperationEnum.delete)
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    public ResultJson<?> deleteBrand(@RequestParam(name="brandCode") String brandCode) {
        ResultJson<?> res = brandService.deleteBrand(brandCode);
        return res;
    }
    @ApiOperation(code="addBrand",name="添加品牌",description = "添加品牌")
    @AuditLog(object="Brand",operation = "添加品牌",type = OperationEnum.add)
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public ResultJson<?> addBrand(@RequestBody(required=false) Brand brand) {
        ResultJson<?> res = brandService.addBrand(brand);
        return res;
    }

    @ApiOperation(code="getBrandGoodsByProductAndBrand",name="查询品牌集体产品实例",description = "查询品牌集体产品实例")
    @AuditLog(object="BrandGoods",operation = "查询品牌集体产品实例",type = OperationEnum.query)
    @RequestMapping(path = "/getBrandGoodsByProductAndBrand",method = RequestMethod.POST)
    public ResultJson<?> getBrandGoodsByProductAndBrand(@RequestParam(name="productCode") String productCode,@RequestParam(name="brandCode") String brandCode) {
        ResultJson<?> res = brandService.getBrandGoodsByProductAndBrand(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="brandAddProductCheckbox",name="",description = "品牌添加产品多选框专用服务")
    @AuditLog(object="BrandGoods",operation = "品牌添加产品多选框专用服务",type = OperationEnum.query)
    @RequestMapping(path = "/brandAddProductCheckbox",method = RequestMethod.POST)
    public ResultJson<?> brandAddProductCheckbox(@RequestParam(name="productCode") String productCode,@RequestParam(name="brandCode") String brandCode) {
        ResultJson<?> res = brandService.brandAddProductCheckbox(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="updateBrandGoods",name="",description = "品牌跟新或者新加产品和产品实例")
    @AuditLog(object="BrandGoods",operation = "品牌跟新或者新加产品和产品实例",type = OperationEnum.query)
    @RequestMapping(path = "/updateBrandGoods",method = RequestMethod.POST)
    public ResultJson<?> updateBrandGoods(@RequestBody(required=true) SaveBrandGoodsVO vo) {
        ResultJson<?> res = brandService.updateBrandGoods(vo);
        return res;
    }

    @ApiOperation(code="getUnassignedBrand",name="",description = "品牌跟新或者新加产品和产品实例")
    @AuditLog(object="brand",operation = "品牌跟新或者新加产品和产品实例",type = OperationEnum.query)
    @RequestMapping(path = "/getUnassignedBrand",method = RequestMethod.POST)
    public ResultJson<?> getUnassignedBrand() {
        ResultJson<?> res = brandService.getUnassignedBrand();
        return res;
    }

    @ApiOperation(code="addBrandProductPriceRule",name="添加更新品牌产品的价格规则",description = "添加更新品牌产品的价格规则")
    @AuditLog(object="Brand",operation = "添加更新品牌产品的价格规则",type = OperationEnum.add)
    @RequestMapping(path = "/product/price/rule/saveOrUpdate",method = RequestMethod.POST)
    public ResultJson<?> saveOrUpdateBrandProductPriceRule(@RequestBody(required=true) List<PriceRule> priceRules) {
        ResultJson<?> res = brandService.saveOrUpdateBrandProductPriceRule(priceRules);
        return res;
    }
    @ApiOperation(code="queryBrandProductPriceRule",name="查询品牌产品的价格规则",description = "查询品牌产品的价格规则")
    @AuditLog(object="Brand",operation = "查询品牌产品的价格规则",type = OperationEnum.add)
    @RequestMapping(path = "/product/price/rule/query",method = RequestMethod.GET)
    public ResultJson<?> queryBrandProductPriceRule(@RequestParam(name="productCode") String productCode,@RequestParam(name="brandCode") String brandCode) {
        ResultJson<?> res = brandService.queryBrandProductPriceRule(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="calculatePrice",name="计算品牌产品(商品)价格",description = "计算品牌产品(商品)价格")
    @AuditLog(object="ProductPrice",operation = "计算品牌产品(商品)价格",type = OperationEnum.query)
    @RequestMapping(path = "/calculatePrice",method = RequestMethod.POST)
    public ResultJson<?> calculatePrice(@RequestBody(required=true)BrandProduct brandProduct) {
        ResultJson<?> res = priceService.calculatePrice(brandProduct);
        return res;
    }


    @ApiOperation(code="getUploadFileSchame",name="获取品牌产品上传价格模板",description = "查询品牌产品商品",type = "common")
    @AuditLog(object="getUploadFileSchame",operation = "获取品牌产品上传价格模板",type = OperationEnum.query)
    @RequestMapping(path = "/getUploadFileSchame",method = RequestMethod.GET)
    public void getUploadFileSchame(@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Brand brand = brandService.getBrandIntanceByBrandCode(brandCode);
        Product product = productService.getProductDetailByProductCode(productCode);
        List<String> head = brandService.getHead(product, brandCode);
        if(CollectionUtils.isEmpty(head)){
            return;
        }
        List<BrandGoods> goodsdata = (List<BrandGoods>)(brandService.getBrandGoodsByProductAndBrand(productCode, brandCode).getData());
        List<List<Object>> data = new ArrayList<>();
        List<Spec> speclist = product.getProductSpecList();
        int specSize = speclist.size();
        for (BrandGoods goods : goodsdata) {
            List<Object> line = new ArrayList<>();
            int index = 0;
            line.add(brand.getBrandName());
            index = index + 1;
            line.add(product.getProductName());
            List<GoodsSpec> goodspecList = goods.getGoodsSpecList();
            for (int i = 0; i < specSize; i++) {
                String specName = head.get(index + 1);
                index = index + 1;
                for (GoodsSpec goodspec : goodspecList) {
                    if (goodspec.getSpecName().toString().equals(specName)) {
                        line.add(goodspec.getSpecValue()+"");
                        break;
                    }
                }
            }
            for(;index<head.size();index++){
                line.add("");
            }
            data.add(line);
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=priceSchame.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ExcelUtil.writeExcelNoMapping(data, out, head);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    @ApiOperation(code="uploadPrice",name="",description = "产品合伙人上传价格文件",type = "common")
    @AuditLog(object="file",operation = "产品合伙人上传价格文件",type = OperationEnum.add)
    @RequestMapping(path = "/uploadPrice",method = RequestMethod.POST)
    public ResultJson<?> uploadPrice(@RequestParam("file") MultipartFile file, @RequestParam(required=true,name="productCode") String  productCode, @RequestParam(required=true,name="brandCode") String  brandCode) throws IOException {
        List<List<String>> result = (List<List<String>>) ExcelUtil.excelRead(file.getInputStream());
        brandService.parsePrice(result,brandCode,productCode);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }


}
